home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / os / sprite / RCS / WaitFor.c,v < prev    next >
Encoding:
Text File  |  1992-08-26  |  8.3 KB  |  368 lines

  1. head     1.4;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    jhh:1.4; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.4
  10. date     92.08.26.13.08.03;  author shirriff;  state Exp;
  11. branches ;
  12. next     1.3;
  13.  
  14. 1.3
  15. date     91.10.19.13.29.18;  author mendel;  state Exp;
  16. branches ;
  17. next     1.2;
  18.  
  19. 1.2
  20. date     90.04.24.10.31.19;  author tve;  state Exp;
  21. branches ;
  22. next     1.1;
  23.  
  24. 1.1
  25. date     90.02.14.19.25.12;  author tve;  state Exp;
  26. branches ;
  27. next     ;
  28.  
  29.  
  30. desc
  31. @Original X11R4 distribution
  32. @
  33.  
  34.  
  35. 1.4
  36. log
  37. @Made it exit if we get EIO, rather than going into an infinite loop.
  38. @
  39. text
  40. @/***********************************************************
  41. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
  42. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  43.  
  44.                         All Rights Reserved
  45.  
  46. Permission to use, copy, modify, and distribute this software and its 
  47. documentation for any purpose and without fee is hereby granted, 
  48. provided that the above copyright notice appear in all copies and that
  49. both that copyright notice and this permission notice appear in 
  50. supporting documentation, and that the names of Digital or MIT not be
  51. used in advertising or publicity pertaining to distribution of the
  52. software without specific, written prior permission.  
  53.  
  54. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  55. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  56. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  57. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  58. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  59. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  60. SOFTWARE.
  61.  
  62. ******************************************************************/
  63.  
  64. /*****************************************************************
  65.  * OS Depedent input routines:
  66.  *
  67.  *  WaitForSomething,  GetEvent
  68.  *
  69.  *****************************************************************/
  70.  
  71. #include "Xos.h"            /* for strings, fcntl, time */
  72.  
  73. #include <errno.h>
  74. #include <stdio.h>
  75. #include "X.h"
  76. #include "misc.h"
  77.  
  78. #include <sys/param.h>
  79. #include <signal.h>
  80. #include "osdep.h"
  81. #include "dixstruct.h"
  82. #include "opaque.h"
  83.  
  84. extern long AllSockets[];
  85. extern long AllClients[];
  86. extern long LastSelectMask[];
  87. extern long WellKnownConnections;
  88. extern long EnabledDevices[];
  89. extern long ClientsWithInput[];
  90. extern long ClientsWriteBlocked[];
  91. extern long OutputPending[];
  92.  
  93. extern long ScreenSaverTime;               /* milliseconds */
  94. extern long ScreenSaverInterval;               /* milliseconds */
  95. extern int ConnectionTranslation[];
  96.  
  97. extern Bool NewOutputPending;
  98. extern Bool AnyClientsWriteBlocked;
  99.  
  100. extern void CheckConnections();
  101. extern void EstablishNewConnections();
  102. extern void SaveScreens();
  103.  
  104. extern int errno;
  105.  
  106. #ifdef apollo
  107. extern long apInputMask[];
  108.  
  109. static long LastWriteMask[mskcnt];
  110. #endif
  111.  
  112. #ifdef XTESTEXT1
  113. /*
  114.  * defined in xtestext1dd.c
  115.  */
  116. extern int playback_on;
  117. #endif /* XTESTEXT1 */
  118.  
  119. /*****************
  120.  * WaitForSomething:
  121.  *     Make the server suspend until there is
  122.  *    1. data from clients or
  123.  *    2. input events available or
  124.  *    3. ddx notices something of interest (graphics
  125.  *       queue ready, etc.) or
  126.  *    4. clients that have buffered replies/events are ready
  127.  *
  128.  *     If the time between INPUT events is
  129.  *     greater than ScreenSaverTime, the display is turned off (or
  130.  *     saved, depending on the hardware).  So, WaitForSomething()
  131.  *     has to handle this also (that's why the select() has a timeout.
  132.  *     For more info on ClientsWithInput, see ReadRequestFromClient().
  133.  *     pClientsReady is an array to store ready client->index values into.
  134.  *****************/
  135.  
  136. static long timeTilFrob = 0;        /* while screen saving */
  137.  
  138. #if (mskcnt>4)
  139. /*
  140.  * This is a macro if mskcnt <= 4
  141.  */
  142. ANYSET(src)
  143.     long    *src;
  144. {
  145.     int i;
  146.  
  147.     for (i=0; i<mskcnt; i++)
  148.     if (src[ i ])
  149.         return (TRUE);
  150.     return (FALSE);
  151. }
  152. #endif
  153. extern void sunInputAvail();
  154.  
  155. int
  156. WaitForSomething(pClientsReady)
  157.     int *pClientsReady;
  158. {
  159.     int i;
  160.     struct timeval waittime, *wt;
  161.     long timeout;
  162.     long clientsReadable[mskcnt];
  163.     long clientsWritable[mskcnt];
  164.     long curclient;
  165.     int selecterr;
  166.     int nready;
  167.     long devicesReadable[mskcnt];
  168.  
  169.     CLEARBITS(clientsReadable);
  170.  
  171.     /* We need a while loop here to handle 
  172.        crashed connections and the screen saver timeout */
  173.     while (1)
  174.     {
  175.     if (ANYSET(ClientsWithInput))
  176.     {
  177.         COPYBITS(ClientsWithInput, clientsReadable);
  178.         break;
  179.     }
  180.     if (ScreenSaverTime)
  181.     {
  182.         timeout = ScreenSaverTime - TimeSinceLastInputEvent();
  183.         if (timeout <= 0) /* may be forced by AutoResetServer() */
  184.         {
  185.         long timeSinceSave;
  186.  
  187.         timeSinceSave = -timeout;
  188.         if ((timeSinceSave >= timeTilFrob) && (timeTilFrob >= 0))
  189.         {
  190.             SaveScreens(SCREEN_SAVER_ON, ScreenSaverActive);
  191.             if (ScreenSaverInterval)
  192.             /* round up to the next ScreenSaverInterval */
  193.             timeTilFrob = ScreenSaverInterval *
  194.                 ((timeSinceSave + ScreenSaverInterval) /
  195.                     ScreenSaverInterval);
  196.             else
  197.             timeTilFrob = -1;
  198.         }
  199.         timeout = timeTilFrob - timeSinceSave;
  200.         }
  201.         else
  202.         {
  203.         if (timeout > ScreenSaverTime)
  204.             timeout = ScreenSaverTime;
  205.         timeTilFrob = 0;
  206.         }
  207.         if (timeTilFrob >= 0)
  208.         {
  209.         waittime.tv_sec = timeout / MILLI_PER_SECOND;
  210.         waittime.tv_usec = (timeout % MILLI_PER_SECOND) *
  211.                     (1000000 / MILLI_PER_SECOND);
  212.         wt = &waittime;
  213.         }
  214.         else
  215.         {
  216.         wt = NULL;
  217.         }
  218.     }
  219.     else
  220.         wt = NULL;
  221.     COPYBITS(AllSockets, LastSelectMask);
  222. #ifdef apollo
  223.         COPYBITS(apInputMask, LastWriteMask);
  224. #endif
  225.     BlockHandler((pointer)&wt, (pointer)LastSelectMask);
  226.     if (NewOutputPending)
  227.         FlushAllOutput();
  228. #ifdef XTESTEXT1
  229.     /* XXX how does this interact with new write block handling? */
  230.     if (playback_on) {
  231.         wt = &waittime;
  232.         XTestComputeWaitTime (&waittime);
  233.     }
  234. #endif /* XTESTEXT1 */
  235.     /* keep this check close to select() call to minimize race */
  236.     if (dispatchException)
  237.         i = -1;
  238.     else if (AnyClientsWriteBlocked)
  239.     {
  240.         COPYBITS(ClientsWriteBlocked, clientsWritable);
  241.         i = select (MAXSOCKS, (int *)LastSelectMask,
  242.             (int *)clientsWritable, (int *) NULL, wt);
  243.     }
  244.     else
  245. #ifdef apollo
  246.         i = select (MAXSOCKS, (int *)LastSelectMask,
  247.             (int *)LastWriteMask, (int *) NULL, wt);
  248. #else
  249.         i = select (MAXSOCKS, (int *)LastSelectMask,
  250.             (int *) NULL, (int *) NULL, wt);
  251. #endif
  252.     selecterr = errno;
  253.     WakeupHandler((unsigned long)i, (pointer)LastSelectMask);
  254. #ifdef XTESTEXT1
  255.     if (playback_on) {
  256.         i = XTestProcessInputAction (i, &waittime);
  257.     }
  258. #endif /* XTESTEXT1 */
  259.     if (i <= 0) /* An error or timeout occurred */
  260.     {
  261.         if (dispatchException)
  262.         return 0;
  263.         CLEARBITS(clientsWritable);
  264.         if (i < 0) 
  265.         if (selecterr == EBADF)    /* Some client disconnected */
  266.         {
  267.             CheckConnections ();
  268.             if (! ANYSET (AllClients))
  269.             return 0;
  270.         }
  271.         else if (selecterr != EINTR)
  272.             ErrorF("WaitForSomething(): select: errno=%d\n",
  273.             selecterr);
  274.             if (selecterr==EIO) {
  275.             /*
  276.              * I don't think we can get out of this alive.
  277.              */
  278.             exit(-1);
  279.             }
  280.     }
  281.     else
  282.     {
  283.         if (AnyClientsWriteBlocked && ANYSET (clientsWritable))
  284.         {
  285.         NewOutputPending = TRUE;
  286.         ORBITS(OutputPending, clientsWritable, OutputPending);
  287.         UNSETBITS(ClientsWriteBlocked, clientsWritable);
  288.         if (! ANYSET(ClientsWriteBlocked))
  289.             AnyClientsWriteBlocked = FALSE;
  290.         }
  291.  
  292.         MASKANDSETBITS(devicesReadable, LastSelectMask, EnabledDevices);
  293. #ifdef    hpux
  294.             /* call the HIL driver to gather inputs.     */
  295.         if (ANYSET(devicesReadable)) store_inputs (devicesReadable);
  296. #endif /* hpux */
  297.  
  298.         MASKANDSETBITS(clientsReadable, LastSelectMask, AllClients); 
  299.         if (LastSelectMask[0] & WellKnownConnections) 
  300.         EstablishNewConnections();
  301.         if (ANYSET (devicesReadable) || ANYSET (clientsReadable))
  302.         break;
  303.     }
  304.     }
  305.  
  306.     nready = 0;
  307.     if (ANYSET(clientsReadable))
  308.     {
  309.     for (i=0; i<mskcnt; i++)
  310.     {
  311.         while (clientsReadable[i])
  312.         {
  313.         curclient = ffs (clientsReadable[i]) - 1;
  314.         pClientsReady[nready++] = 
  315.             ConnectionTranslation[curclient + (i << 5)];
  316.         clientsReadable[i] &= ~(1 << curclient);
  317.         }
  318.     }    
  319.     }
  320. #if defined(sun4) || defined(sun3)
  321. /* Added by TvE (hack) : */
  322.     if(ANYSET(devicesReadable))
  323.         sunInputAvail();
  324. /* End TvE hack */
  325. #endif
  326.     return nready;
  327. }
  328.  
  329.  
  330.  
  331. @
  332.  
  333.  
  334. 1.3
  335. log
  336. @*** empty log message ***
  337. @
  338. text
  339. @d235 6
  340. @
  341.  
  342.  
  343. 1.2
  344. log
  345. @*** empty log message ***
  346. @
  347. text
  348. @d114 1
  349. d275 1
  350. a275 1
  351.  
  352. d280 1
  353. a280 1
  354.  
  355. a283 1
  356. extern void sunInputAvail();
  357. @
  358.  
  359.  
  360. 1.1
  361. log
  362. @Initial revision
  363. @
  364. text
  365. @d274 6
  366. d283 1
  367. @
  368.